/**
 * 静态分析相关管理路由
 *
 * Created by yangyunze on 2021/05/31.
 */
const PhoenixError = require('../../models/phoenix-error');
const middleware = require('./../middleware');
const defects = require('../../models/defects');

function overview(req, res, next) {
  const {
    userId,
    projectName,
  } = req.query;
  defects.getAllVersionsOverview({
    ...req.query,
    lastProjectOnly: !userId,
    lastVersionOnly: !(userId && projectName),
  })
    .then((ret) => res.json(ret))
    .catch((err) => {
      next(new PhoenixError(err));
    });
}

/**
 * Export model definition object.
 */
module.exports = (app, prefix) => {
  const overviewURL = `${prefix}/static-analyze/overview.json`;

  // 获取缺陷列表
  app.get(overviewURL, middleware.user.requireTokenUnexpired);
  app.get(overviewURL, middleware.user.requireAvailable);
  app.get(overviewURL, middleware.user.requireTestManager);
  app.get(overviewURL, overview);
};
